package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.console.excel.ExportExcel;
import com.qumitech.visualnovel.console.excel.StateField;
import com.qumitech.visualnovel.console.excel.State;
import com.qumitech.visualnovel.console.excel.UnitField;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.controller.vo.shop.OrderExcelVO;
import com.qumitech.visualnovel.controller.vo.shop.ShopAdminAuditDTO;
import com.qumitech.visualnovel.controller.vo.shop.ShopOrder;
import com.qumitech.visualnovel.controller.vo.shop.ShopOrderQuery;
import com.qumitech.visualnovel.controller.vo.shop.ShopOrderSupplementReq;
import com.qumitech.visualnovel.pubdef.dto.Shop.SupplememtDTO;
import com.qumitech.visualnovel.service.shop.ShopService;
import com.google.common.collect.ImmutableMap;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qumitech.visualnovel.console.excel.ExportExcel;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/shop")
public class ShopController {
    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/order/list", method = RequestMethod.GET)
    @ExportExcel(itemCls = OrderExcelVO.class,
        listKey = "orders",
            unitFields = {@UnitField(key="price", unit = 100)},
            stateFields = {
                @StateField(key="payType", states = {
                    @State(state="0",display="控制台支付"),
                    @State(state="1",display="支付宝"),
                    @State(state="2",display="微信"),
                    @State(state="3",display="代付宝"),
                    @State(state="4",display="打卡余额"),
                    @State(state="5",display="苹果"),
                    @State(state="6",display="钻石"),
                    @State(state="7",display="支付宝H5"),
                    @State(state="8",display="微信H5"),
                    @State(state="10",display="收益兑换"),
                    @State(state="11",display="微信公众号支付"),
                    @State(state="12",display="贝付宝微信H5支付"),
                    @State(state="13",display="返点兑换"),
                    @State(state="14",display="天天支付宝H5支付"),
                    @State(state="15",display="贝付宝支付宝支付")}),
            @StateField(key="state", states = {
                    @State(state="1", display = "未支付"),
                    @State(state="2", display = "已支付")
            })}
    )
    public BaseRsp getOrders(ShopOrderQuery query) {
        Map<String, Object> rspMap = shopService.getOrders(query);
        int count = (int) rspMap.get("count");
        List<ShopOrder> orders = (List<ShopOrder>) rspMap.get("orders");
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "orders", orders, "sum", rspMap.getOrDefault("sum", 0)));
    }

    @RequestMapping(value = "/order/{oid}/supplement", method = RequestMethod.PUT)
    public BaseRsp supplementOrder(@PathVariable("oid") String oid, @RequestBody ShopOrderSupplementReq req) {
        shopService.supplementOrder(oid, req.getRemark());
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @RequestMapping(value = "/order/supplement", method = RequestMethod.POST)
    public BaseRsp supplementOrder(@RequestBody SupplememtDTO supplememtDTO) {
        shopService.addSupplememt(supplememtDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @RequiresPermissions("sendReward:add")
    @PostMapping(value = "/reward")
    public BaseRsp saveReward(@RequestBody JSONObject param) {
        shopService.saveReward(param);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @PostMapping(value = "/reward/userInfo")
    public BaseRsp rewardUserInfo(@RequestBody JSONObject param) {
        return shopService.rewardUserInfo(param);
    }

    @RequiresPermissions("sendRewardList:view")
    @RequestMapping(value = "/rewardList", method = RequestMethod.GET)
    @ExportExcel(listKey="prop_list",
      itemKeys={"uid", "user_name", "prop_type", "amount", "change_type", "remark", "operator", "create_time"},
      itemNames={"内部ID", "用户名称", "货币类型", "奖励数量", "奖励类型", "奖励说明", "操作人", "操作时间"},
      stateFields = {
            @StateField(key="prop_type", states = {
                @State(state="1", display="金币"),
                @State(state="2", display="钻石"),
                @State(state="3", display="嗨币")}),
            @StateField(key="change_type", states = {
                @State(state="18", display="活动奖励"),
                @State(state="20", display="其它")}) }
    )
    public BaseRsp getRewardList(@RequestParam(value = "startTime") String startTime,
                                 @RequestParam(value = "endTime") String endTime,
                                 @RequestParam(value = "offset", required = false, defaultValue = "0") int offset,
                                 @RequestParam(value = "limit", required = false, defaultValue = "20") int limit,
                                 @RequestParam(value = "operator", required = false) String operator,
                                 @RequestParam(value = "uid", required = false) Long uid,
                                 @RequestParam(value = "propType", required = false) Integer propType,
                                 @RequestParam(value = "changeType", required = false) Integer changeType) {
        return shopService.getRewardList(operator, uid, propType, changeType, startTime, endTime, offset, limit);
    }


    @RequestMapping(value = "/order/audit", method = RequestMethod.PUT)
    public BaseRsp supplementOrder(@RequestBody ShopAdminAuditDTO req) {
        shopService.audit(req);
        return new BaseRsp(ResultEnum.SUCCESS);
    }
}
